package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.NbcbB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.NbcbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.nbcb.ebank.NbcbB2cMessage;
import com.gopay.common.domain.bank.nbcb.ebank.query.req.NbcbB2cReqNetPay;
import com.gopay.common.domain.bank.nbcb.ebank.query.res.NbcbB2cResList;
import com.gopay.common.domain.bank.nbcb.ebank.query.res.NbcbB2cResNetPay;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class NbcbB2cBankServiceImpl implements BankInteractRemoteService {
	
	@Resource(name = "nbcbB2cManager")
	private NbcbB2cManager nbcbB2cManager;
	@Override
	public Result pay(BaseMessage base){
		Result result = new Result(false);
		try{
			NbcbB2cMessage messageB2C = new NbcbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			messageB2C = (NbcbB2cMessage) nbcbB2cManager.sign(messageB2C);
			result.setDefaultModel(messageB2C);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			// B2C验签
			NbcbB2cMessage messageB2C = new NbcbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			boolean flag = (Boolean) nbcbB2cManager.verify(messageB2C);
			result.setSuccess(true);
			result.setDefaultModel(flag);
			result.setModel("res", messageB2C);
			// 验签成功入账
			if (flag) {
				Map<String, String> param = new HashMap<String, String>();
				if (messageB2C.getTranResult().equals("0")) {
					// 通知后台地址入账					
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "00");
					param.put("retCode", "00");
					param.put("retMsg", "验签成功");
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", base.isFront()+"");
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(true);
				} else {
					// 通知后台地址该订单失败
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "01");
					param.put("retCode", "01");
					param.put("retMsg", "验签失败");
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", base.isFront()+"");
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(false);
				}
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			// B2C查询
			NbcbB2cReqNetPay messageB2C = new NbcbB2cReqNetPay();
			BeanUtils.copyProperties(base, messageB2C);
			NbcbB2cResNetPay repMessage = (NbcbB2cResNetPay) nbcbB2cManager.query(messageB2C);
			result.setDefaultModel(repMessage);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getAmount().toString());
				if (repMessage == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				List<NbcbB2cResList> repList = repMessage.getRep();
				NbcbB2cResList rep = new NbcbB2cResList();
				for(int i=0; i<repList.size(); i++){
					rep = (NbcbB2cResList)repList.get(i);
				}
				String orderStatus =rep.getTranResult(); // 交易状态
				//成功处理
				if (orderStatus.equals(NbcbConstants.ORDERSTATUS_SUCC)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
					param.put("returnMessage", "交易成功");
				}
				//失败处理
				else if (orderStatus.equals(NbcbConstants.ORDERSTATUS_FAIL)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
					param.put("returnMessage", "交易失败");
				}
				else if (orderStatus.equals(NbcbConstants.ORDERSTATUS_DOU) || orderStatus.equals(NbcbConstants.ORDERSTATUS_NOFLW) || orderStatus.equals(NbcbConstants.CHECK_FAIL)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行返回交易待确认");
				}else {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行返回交易未明情况");
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
